package com.beichen.service;

import com.beichen.pojo.CertificateInfo;
import com.beichen.util.PdfToImageConverterUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;

@Service
public class CertificateService {

    @Autowired
    private  AliyunModelService aliyunModelService;
    @Autowired
    private  PdfToImageConverterUtil pdfToImageConverter;



    public CertificateService() {
    }

    public  CertificateInfo recognize(String filePath) throws Exception {
        System.out.println("=== CertificateService.recognize ===");
        System.out.println("接收到的文件路径: " + filePath);

        // 处理文件路径（确保是本地路径）
        String actualPath = filePath.replace("file://", "");
        File sourceFile = new File(actualPath);

        // 检查文件类型并处理
        if (isPdfFile(sourceFile)) {
            System.out.println("检测到PDF文件，开始转换...");
            File imageFile = pdfToImageConverter.convertToImage(sourceFile);
            System.out.println("PDF转换完成，生成图片: " + imageFile.getAbsolutePath());
            return aliyunModelService.recognizeCertificate(imageFile.getAbsolutePath());
        } else {
            System.out.println("直接处理图片文件");
            return aliyunModelService.recognizeCertificate(actualPath);
        }
    }

    public   boolean isPdfFile(File file) {
        String name = file.getName();
        return name != null && name.toLowerCase().endsWith(".pdf");
    }
}